part of AppUtils;

UserModel? globalUserModel;

extension User on _AppUtils {
  Future<void> initUserModel() async {
    var userModel = await yRead("userModel");
    globalUserModel = (userModel == null ? null : UserModel.fromJson(json.decode(userModel)));
    reLoadPages();
  }

  void saveUserModel(UserModel userModel) {
    ySave("userModel", userModel.toJson());
    globalUserModel = userModel;
    reLoadPages();
  }

  void deleteUserModel() {
    yDelete("userModel");
    globalUserModel = null;
    reLoadPages();
  }

  Future<bool> checkIsLogon(YPage page, {bool goLogin = false}) async {
    if (globalUserModel != null)
      return true;
    else {
      if (goLogin) page.yIntent(LoginPage(), intentType: YIntentType.Present);
      return false;
    }
  }
}
